package com.cmcc.linlaker.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.LoginActivity;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.bean.BaseBean;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.HashMap;
import java.util.Map;

public class ModifyPwdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);

        BarUtils.initStatusBar(this);


        initView();
    }

    private EditText edit_old_pwd, edit_new_pwd, edit_new_pwd_again;

    private void initView() {
        edit_old_pwd = findViewById(R.id.edit_old_pwd);
        edit_new_pwd = findViewById(R.id.edit_new_pwd);
        edit_new_pwd_again = findViewById(R.id.edit_new_pwd_again);
    }


    /**
     * 完成
     */
    public void finish(View view) {
        String oldPwd = edit_old_pwd.getText().toString();
        String newPwd = edit_new_pwd.getText().toString();
        String newPwdAgain = edit_new_pwd_again.getText().toString();

        if (TextUtils.isEmpty(oldPwd)) {
            Toast.makeText(this, "请输入旧密码", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(newPwd)) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
        } else if (newPwd.length() < 6) {
            Toast.makeText(this, "请输入大于6位新密码", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(newPwdAgain)) {
            Toast.makeText(this, "请确认新密码", Toast.LENGTH_SHORT).show();
        } else if (!newPwd.equals(newPwdAgain)) {
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        } else {
            resetPwd(oldPwd, newPwd);
        }
    }

    /**
     * 修改密码
     */
    private void resetPwd(String oldPwd, String pwd) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("loginName", SPHelper.getUsername());
        params.put("password", pwd);
        params.put("orderPwd", oldPwd);
        helper.sendPOSTRequest(ApiDefine.MODIFY_PWD, BaseBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    BaseBean bean = (BaseBean) data;
                    if (bean.code.equals("0")) {
                        SPHelper.setAuthToken("");
                        SPHelper.setPassword("");
                        ToastUtils.showText(ModifyPwdActivity.this, bean.msg);
                        Intent intent = new Intent(ModifyPwdActivity.this, LoginActivity.class);
                        intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TASK | intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(ModifyPwdActivity.this);
                    } else {
                        ToastUtils.showText(ModifyPwdActivity.this, bean.msg);
                    }
                }
            }
        });
    }

    public void back(View view) {
        finish();
    }

}
